<?php
namespace Admin\Controller;
use Think\Controller;

class GoodsController extends CommonController{

    private $Goods="";      // GOODS 产品表
    private $GoodsGallery="";     //Godds_gallery 相册表
    private $Category="";     // 栏目表

    function __construct()
    {
        parent::__construct();
        $this->Goods=D('Goods');
        $this->GoodsGallery=D('Goods_gallery');
        $this->Category=D("Category");
        $this->Goods->startTrans();
    }

    /*
     * 商品列表页
     *
     *  查找出必要的字段
     *  <th>ID</th>
        <th>商品编码</th>
        <th>名称</th>
        <th>促销</th>
        <th>精品</th>
        <th>新品</th>
        <th>热销</th>
        <th>updateTime</th>
     * */
    function index()
    {
        if($_POST['cate_id'])
        {
            if($_POST['cate_id']!="all")
            {
                $condGoods=$this->Goods->where("cate_id=".$_POST['cate_id'])->order(array('order','id'=>'desc'))->select();
            }else{
                $condGoods=$this->Goods->field('id,goods_sn,name,is_promote,is_best,is_new,is_hot,last_update,order')->order(array('order','id'=>'desc'))->select();
            }
            $this->assign("data",$condGoods);
        }else{
            $allGoods=$this->Goods->field('id,goods_sn,name,is_promote,is_best,is_new,is_hot,last_update,order')->order(array('order','id'=>'desc'))->select();
            $this->assign("data",$allGoods);
        }
        $allCate=$this->Category->field("id,name")->select();
        $this->assign("cate",$allCate);
        $this->display();
    }

    function sort()
    {
        $goodsID=$_POST['goods_id'];
        $sort=$_POST['sort_val'];
        $yanG1=$this->Goods->select();

        $oldGoodsIDSort=$this->Goods->where("id=".$goodsID)->find();
        $data['order']=$sort;
        $res1=$this->Goods->where("id=".$goodsID)->save($data);
        if($res1)
        {
            $this->Goods->commit();
        }else{
            $this->Goods->rollback();
        }
//        foreach($yanG1 as $key=>$r)
//        {
//            // 排序 调换操作
//            if($sort==$r['order'])
//            {
//                $dataa['order']=$oldGoodsIDSort['order'];
//                $res1=$this->Goods->where("id=".$r['id'])->save($dataa);
//                if($res1)
//                {
//                    $this->Goods->commit();
//                }else{
//                    $this->Goods->rollback();
//                }
//            }
//        }
        echo "ok"; exit;
    }

    // 判断 商品编号是否重复，保证商品编号的唯一性
    function prompeSN($sn='')
    {
        $cond['goods_sn']=$sn;
        $yanG1=$this->Goods->where($cond)->select();
        if($yanG1=="")
        {
            return true;
        }else{
            return false;
        }
    }

    /*
     * 商品添加
     * */
    function addGoods()
    {
        if($_POST)
        {
            $data=$_POST;
            $data['id']='';

                $data['goods_sn']=$_POST['goods_sn'];


            if($data['is_promote']==1)     // 当 is_promote=1 时，表示开启促销 ，再去修改时间格式
            {
                $promote_start_date=$_POST['promote_start_date'];
                $yanG1=explode("-",$promote_start_date);
                $yanGstart=mktime(0,0,0,$yanG1[0],$yanG1[1],$yanG1[2]);      // 得到促销开始的时间戳

                $promote_end_date=$_POST['promote_end_date'];
                $yanG2=explode("-",$promote_end_date);
                $yanGend=mktime(0,0,0,$yanG2[0],$yanG2[1],$yanG2[2]);      // 得到促销结束的时间戳

                if($yanGstart>=$yanGend||empty($yanGstart)||empty($yanGend))
                {
                    $this->error("促销时间设置有问题!!!");
                }else{
                    $data['promote_start_date']=$yanGstart;
                    $data['promote_end_date']=$yanGend;
                }
            }      //促销部处理完成

            //  缩略图的上传
            if(!empty($_FILES['thumb']['name']))
            {
                $data['thumb'] = $this->uploadOneImg("thumb",$_POST['goods_sn']);    // 上传缩略图
            }

            if(!empty($_FILES['img']['name']))
            {
                $data['img']=$this->uploadOneImg("img",$_POST['goods_sn']);     // 上传图片
            }
            // 图上传end

            $data['last_update']=time();     //  最后一次修改的时间
            $data['add_time']=time();        //   添加的时间

            $aa=$this->Goods->data($data)->add();
            if(isset($aa))
            {
                $this->Goods->commit();
                $this->success("商品上传成功");
            }else{
                $this->Goods->rollback();
                $this->error("商品上传失败");
            }
        }else{

            $cate=$this->Category->field(array('id','name'))->select();
            $this->assign("cate",$cate);
            $this->display();
        }
    }

    /*
     * 商品修改
     * */
    function updataGoods($id='')
    {
        if($_POST)
        {
            $data=$_POST;
//            $prompeSN=$this->prompeSN($_POST['goods_sn']);
//            if($prompeSN==true)
//            {
                $data['goods_sn']=$_POST['goods_sn'];


            if($data['is_promote']==1)     // 当 is_promote=1 时，表示开启促销 ，再去修改时间格式
            {
                $promote_start_date=$_POST['promote_start_date'];
                $yanG1=explode("-",$promote_start_date);
                $yanGstart=mktime(0,0,0,$yanG1[0],$yanG1[1],$yanG1[2]);      // 得到促销开始的时间戳

                $promote_end_date=$_POST['promote_end_date'];
                $yanG2=explode("-",$promote_end_date);
                $yanGend=mktime(0,0,0,$yanG2[0],$yanG2[1],$yanG2[2]);      // 得到促销结束的时间戳

                if($yanGstart>=$yanGend||empty($yanGstart)||empty($yanGend))
                {
                    $this->error("促销时间设置有问题!!!");

                }else{
                    $data['promote_start_date']=$yanGstart;
                    $data['promote_end_date']=$yanGend;
                }
            }      //促销部处理完成

            //  缩略图的上传
            if(!empty($_FILES['thumb']['name']))
            {
                $data['thumb'] = $this->uploadOneImg("thumb",$_POST['goods_sn']);    // 上传缩略图
            }

            if(!empty($_FILES['img']['name']))
            {
                $data['img']=$this->uploadOneImg("img",$_POST['goods_sn']);     // 上传图片
            }
            // 图上传end

            $data['last_update']=time();     //  最后一次修改的时间
            $data['add_time']=time();        //   添加的时间

            $aa=$this->Goods->where('id='.$id)->save($data);
            if(isset($aa))
            {
                $this->Goods->commit();
                $this->success("商品修改成功");
            }else{
                $this->Goods->rollback();
                $this->error("商品修改失败");
            }
        }else{
            $arr=$this->Goods->where("id=".$id)->find();
            $data=$arr;
            if($arr['is_promote']=="1")    // 是否开启了促销
            {
                $data['promote_start_date']=date("m-d-Y",$arr['promote_start_date']);   // 促销起始时间
                $data['promote_end_date']=date("m-d-Y",$arr['promote_end_date']);   // 促销结束时间
            }
            $data['add_time']=date("Y-m-d",$arr['add_time']);
            $data['last_update']=date("Y-m-d",$arr['last_update']);

            /*
             * id , goods , url
             * */
            $brr=$this->GoodsGallery->field(array('id,goods_id,url'))->where('goods_id='.$id)->select();
            $cate=$this->Category->field(array('id','name'))->select();
            $this->assign("cate",$cate);
            $this->assign("gallery",$brr);
            $this->assign("data",$data);
            $this->display();
        }
    }


    /*
     * 商品的删除
     *$id为 商品ID
     * */
    function delGoods($id=''){

        //删除此上皮难度图集
        $arr=$this->GoodsGallery->field("url")->where("goods_id=".$id)->select();

        foreach($arr as $r)
        {
            print_r($r['url']);
            $yanG2="../ddgc/Uploads{$r['url']}";
            if(file_exists($yanG2))
            {
                echo "文件存在";
                $yanG3=@unlink($yanG2);    // 删除文件夹中的文件
                if($yanG3==false)
                {
                    echo "商品没有从文件夹中删除";
                }else{
                    echo "商品成功删除";
                }
            }else{
                echo "文件不存在";
            }
        }
        $this->GoodsGallery->where("goods_id=".$id)->delete(); //删除图集表中的此商品的数据

        $this->Goods->where("id=".$id)->delete();

        $this->success("商品删除成功");
    }

    /*
     * $id 为商品ID
     *
     * 上传多图
     * */
    function upMoreImg($id='')
    {
        $upload = new \Think\Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg','gif','png','jpeg');
        $upload->savePath='./pic_goods/';      // 设置上传目录
        $upload->subName  = array('date','Ymd');
        $info = $upload->upload();
        if(!$info){
            $this->error($upload->getError());
        }else{
            foreach($info as $file)
            {
                //echo $file['savepath'].$file['savename'];
                $data['id']='';
                $data['goods_id']=$id;
                $yanG1=$file['savepath'].$file['savename'];
                $yanG2=explode(".",$yanG1);
                $data['url']=$yanG2[1].".".$yanG2[2];
                $this->GoodsGallery->data($data)->add();
            }
        }
        $this->redirect('Goods/index');
    }

    /*
     * 单个文件的上传
     * */
    function uploadOneImg($tran='',$goods_sn)         //  %tran 将会得到两个值 thumb 或者 img
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->autoSub = false;
        $upload->replace = true;
        if($tran=='thumb')
        {
            $upload->savePath  = './thumb/'; // 设置附件上传目录
            $upload->saveName  = $goods_sn;
            $info   =   $upload->uploadOne($_FILES['thumb']);
        }
        elseif($tran=='img')
        {
            $upload->savePath  = './pic_goods/'; // 设置附件上传目录
            $upload->saveName  = $goods_sn;
            $info   =   $upload->uploadOne($_FILES['img']);
        }
        else
        {
            echo "上传图片错误，可能后台代码的问题!!";
            exit;
        }

        // 上传单个文件

        if(!$info){// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            //echo $info['savepath'].$info['savename'];
            return $info['savename'];
        }
    }

    /*
     * 删除图片
     * 单个图片的删除
     * $id 图片的唯一ID
     * $url 图片的路径
     * */
    function delOne($id)
    {
        $yanG1=$this->GoodsGallery->field('url')->where('id='.$id)->find();
        //$yanG2="<img src=/thinkphp/Uploads{$yanG1['url']}>";
        $yanG2="../thinkphp/Uploads{$yanG1['url']}";
        if(file_exists($yanG2)){    // 判断文件是否存在
            $yanG3=@unlink($yanG2);    // 删除文件
            if($yanG3==false)
            {
                $this->error("删除失败");//删除失败
            }else{
                $yanG4=$this->GoodsGallery->where("id=".$id)->delete();
                if($yanG4==true)
                {
                    $this->GoodsGallery->commit();
                    $this->success("删除成功");
                }else{
                    $this->GoodsGallery->rollback();
                    $this->error("数据库删除失败");
                }
            }
        }else{
            $res2=$this->GoodsGallery->where("id=".$id)->delete();
            if($res2)
            {
                $this->GoodsGallery->commit();
                $this->error("文件不存在,清理数据库中的残余");
            }else{
                $this->GoodsGallery->rollback();
                $this->error("操作失败");
            }
        }
    }
}
?>